package org.openharmony.qttool;


import org.jetbrains.annotations.Nls;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;

public class QtSDKTableModel implements TableModel {
    private String errorString;

    private final ArrayList<QtSDK> qtSDKList;
    private final ArrayList<QtSDK> removedSDK = new ArrayList<>();
    public QtSDKTableModel() {
        qtSDKList = new ArrayList<>(QtSDKManager.getInstance().allKits());
    }

    @Override
    public int getRowCount() {
        return qtSDKList.size();
    }


    @Override
    public int getColumnCount() {
        return 3;
    }

    @Nls
    @Override
    public String getColumnName(int columnIndex) {
        if (columnIndex == 0)
            return "Version";
        else if (columnIndex == 1)
            return "Arch";
        else if (columnIndex == 2)
            return "Path";
        return null;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

//    ָ�������ɱ༭
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < 0 || rowIndex >= qtSDKList.size())
            return null;
        QtSDK sdk = qtSDKList.get(rowIndex);
        if (sdk == null)
            return null;
        if (columnIndex == 0)
            return sdk.version();
        if (columnIndex == 1)
            return sdk.getArch();
        if (columnIndex == 2)
            return sdk.prefix();
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }

    public boolean add(String sdkPath) {
        QtSDK sdk = new QtSDK(sdkPath);
        if (!sdk.isValid()) {
            errorString = "Invalid Qt SDK";
            return false;
        }
        if (qtSDKList.contains(sdk)) {
            errorString = "The same Qt SDK has Installed";
            return false;
        }
        qtSDKList.add(sdk);
        return true;
    }

    public void remove(int index) {
        if (index < 0 || index >= qtSDKList.size())
            return;
        removedSDK.add(qtSDKList.remove(index));
    }

    public ArrayList<QtSDK> allKits() {
        return qtSDKList;
    }

    public ArrayList<QtSDK> removedKits() {
        return removedSDK;
    }

    public String errorString() {
        return this.errorString;
    }
}
